"use strict";

const csharp_1 = require("csharp");
const puerts_1 = require("puerts");
const CustomAttributer = require("../CustomAttributer/main");
const App = csharp_1.FairyEditor.App;

// 读取配置
let filePath = `${csharp_1.FairyEditor.App.project.basePath}/plugins/LocalizationText/config.json`;
let config;
try {
    let sw = new csharp_1.System.IO.StreamReader(filePath);
    let data = sw.ReadToEnd();
    sw.Close();
    config = JSON.parse(data);
}
catch (e) {
    console.warn(e);
}

if (config == undefined) {
    return;
}

let jsonPath = `${csharp_1.FairyEditor.App.project.basePath}/${config.l10nJsonPath}`;

let loadL10NText = function () {
    let l10nDict = {}
    try {
        let sw = new csharp_1.System.IO.StreamReader(jsonPath);
        let data = sw.ReadToEnd();
        sw.Close();
        let l10nList = JSON.parse(data);
    
        for (var index in l10nList){
            let item = l10nList[index];
            l10nDict[item.key] = item.cn;
        }
    }
    catch (e) {
        console.warn(e);
        return;
    }
    return l10nDict;
}

loadL10NText();

let setL10NText = function (target, l10nKey) {
    let l10nDict = loadL10NText();
    var str = l10nDict[l10nKey];
    if (str == undefined) {
        console.error("l10n key not found:" + l10nKey);
        return;
    }

    let modify = false;

    let objectTypeName = CustomAttributer.getObjectTypeName(target);
    if (objectTypeName == csharp_1.FairyEditor.FObjectType.INPUTTEXT) {
        // 输入文本 设置提示语
        if (target.promptText != str) {
            target.docElement.SetProperty("promptText", str);
            modify = true;
        }
    } else {
        if (target.text == undefined) {
            return;
        }
        
        if (target.text != str) {
            target.docElement.SetProperty("text", str);
            modify = true;
        }
    }

    if (target.clearOnPublish != undefined && target.clearOnPublish == false) {
        target.docElement.SetProperty("clearOnPublish", true);
        modify = true;
    }

    if (modify) {
        App.inspectorView.Refresh("text");
    }
}

CustomAttributer.customHandlers["l10nKey"] = function(instance, component, item, value) {
    instance.renderCommonItem(component, item, value);

    var l10nKey = value + "";
    if (l10nKey == "") {
        return;
    }

    setL10NText(App.activeDoc.inspectingTarget, l10nKey);
}

CustomAttributer.customHandlers["l10nKeyController"] = function(instance, component, item, value) {

    let items = ["无"];
    let values = [""];

    let controllers = App.activeDoc.content.controllers;
    for (var i = 0; i < controllers.Count; i++){
        let controller = controllers.get_Item(i);
        items.push(controller.name);
        values.push(controller.name);
    }

    item.data = {
        items: items,
        values: values
    }

    instance.renderCommonItem(component, item, value);
}

// custom menu
App.menu.AddItem("本地化文本", "l10n", 5, true, function(params) { });

let l10nMenu = App.menu.GetSubMenu("l10n");
l10nMenu.AddItem("更新当前组件", "UpdateL10NForActiveDoc", function(params) {
    let doc = App.activeDoc;
    if (doc == undefined) {
        return;
    }

    let numChildren = doc.content.numChildren;
    for (var index = 0; index < numChildren; ++index) {
        let child = doc.content.GetChildAt(index);

        if (child.customData == undefined || child.customData == "")
            continue;

        let customDataObj;
        try {
            customDataObj = JSON.parse(child.customData) || {};
        }
        catch (e) {
            // console.log("自定义数据异常或没有发现自定义数据，无法渲染列表");
            customDataObj = {};
        }

        let l10nKey = customDataObj["l10nKey"];
        if (l10nKey == undefined) {
            return;
        }

        setL10NText(child, l10nKey);
    }
})

exports.onDestroy = function(){
    App.menu.RemoveItem("l10n");
}